#include "slicingsettingsdialog.h"
#include "ui_slicingsettingsdialog.h"

SlicingSettingsDialog::SlicingSettingsDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::SlicingSettingsDialog)
{
    ui->setupUi(this);
    iniUI();
}

SlicingSettingsDialog::~SlicingSettingsDialog()
{
    delete normalVectorValidator;
    delete positiveFloatValidator;
    delete ui;
}

void SlicingSettingsDialog::iniUI()
{
    // 设置输入验证器（允许小数和负数）
    normalVectorValidator = new QRegularExpressionValidator(QRegularExpression(R"(^-?\d*\.?\d+$)"), this);
    positiveFloatValidator = new QRegularExpressionValidator(QRegularExpression(R"(^[+]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)$)"), this);

    ui->slicingStride->setValidator(positiveFloatValidator);
    ui->ditherCoefficient->setValidator(positiveFloatValidator);
    ui->mergeCoefficient->setValidator(positiveFloatValidator);
    ui->slicingStride->setText("0.001");
    ui->ditherCoefficient->setText("0.01");
    ui->mergeCoefficient->setText("0.001");
}

float SlicingSettingsDialog::slicingStride() const
{
    return ui->slicingStride->text().toFloat();
}

float SlicingSettingsDialog::ditherCoefficient() const
{
    return ui->ditherCoefficient->text().toFloat();
}

float SlicingSettingsDialog::mergeCoefficient() const
{
    return ui->mergeCoefficient->text().toFloat();
}

void SlicingSettingsDialog::on_settingConfirmed_accepted()
{
    // 获取当前设置
    float stride = slicingStride();

    // 发射信号传递数据
    emit slicingSettingsConfirmed(stride);

    // 关闭对话框
    this->accept();
}

void SlicingSettingsDialog::on_settingConfirmed_rejected()
{
    this->close();
}
